{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "\n",
       "require(['notebook/js/codecell'], function(codecell) {\n",
       "  codecell.CodeCell.options_default.highlight_modes[\n",
       "      'magic_text/x-csrc'] = {'reg':[/^%%microblaze/]};\n",
       "  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n",
       "      Jupyter.notebook.get_cells().map(function(cell){\n",
       "          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n",
       "  });\n",
       "});\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pynq import DefaultIP\n",
    "from pynq import Overlay\n",
    "from pynq import Xlnk\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class BlackScholesDriver(DefaultIP):\n",
    "    def __init__(self, description):\n",
    "        super().__init__(description=description)\n",
    "\n",
    "    bindto = ['xilinx.com:hls:black_scholes:1.0']\n",
    "\n",
    "    @property\n",
    "    def status(self):\n",
    "        return self.read(0x00)\n",
    "    \n",
    "    @status.setter\n",
    "    def status(self, value):\n",
    "        self.write(0x00, value)\n",
    "\n",
    "    @property\n",
    "    def output(self):\n",
    "        return self.read(0x10)\n",
    "\n",
    "    @output.setter\n",
    "    def output(self, value):\n",
    "        self.write(0x10, value)\n",
    "\n",
    "    @property\n",
    "    def spot_price(self):\n",
    "        return self.read(0x18)\n",
    "\n",
    "    @spot_price.setter\n",
    "    def spot_price(self, value):\n",
    "        self.write(0x18, value)\n",
    "\n",
    "    @property\n",
    "    def strike_price(self):\n",
    "        return self.read(0x20)\n",
    "\n",
    "    @strike_price.setter\n",
    "    def strike_price(self, value):\n",
    "        self.write(0x20, value)\n",
    "\n",
    "    @property\n",
    "    def time_to_maturity(self):\n",
    "        return self.read(0x28)\n",
    "\n",
    "    @time_to_maturity.setter\n",
    "    def time_to_maturity(self, value):\n",
    "        self.write(0x28, value)\n",
    "\n",
    "    @property\n",
    "    def risk_free_rate(self):\n",
    "        return self.read(0x30)\n",
    "\n",
    "    @risk_free_rate.setter\n",
    "    def risk_free_rate(self, value):\n",
    "        self.write(0x30, value)\n",
    "\n",
    "    @property\n",
    "    def volatility(self):\n",
    "        return self.read(0x38)\n",
    "\n",
    "    @volatility.setter\n",
    "    def volatility(self, value):\n",
    "        self.write(0x38, value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "overlay = Overlay(\"./overlay/black_scholes.bit\")\n",
    "BlackScholes = overlay.black_scholes_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "xlnk = Xlnk()\n",
    "output = xlnk.cma_array(shape=(2), dtype=np.float32)\n",
    "S = xlnk.cma_array(shape=(1), dtype=np.float32)\n",
    "K = xlnk.cma_array(shape=(1), dtype=np.float32)\n",
    "T = xlnk.cma_array(shape=(1), dtype=np.float32)\n",
    "r = xlnk.cma_array(shape=(1), dtype=np.float32)\n",
    "v = xlnk.cma_array(shape=(1), dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "S[0] = 50\n",
    "K[0] = 50\n",
    "T[0] = 1\n",
    "r[0] = 5\n",
    "v[0] = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "ap_start = 1\n",
    "ap_done = 2\n",
    "ap_idle = 4\n",
    "ap_ready = 8\n",
    "\n",
    "BlackScholes.spot_price = S.physical_address\n",
    "BlackScholes.strike_price = K.physical_address\n",
    "BlackScholes.time_to_maturity = T.physical_address\n",
    "BlackScholes.risk_free_rate = r.physical_address\n",
    "BlackScholes.volatility = v.physical_address\n",
    "BlackScholes.output = output.physical_address\n",
    "BlackScholes.status = ap_start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BlackScholes.status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Call price:  5.22529\n",
      "Put price:   2.78676\n"
     ]
    }
   ],
   "source": [
    "print(\"Call price: \", output[0])\n",
    "print(\"Put price:  \", output[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
